home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / games / wordy313.zip / UNSCR.C < prev    next >
C/C++ Source or Header  |  1995-04-30  |  5KB  |  199 lines

  1. /**************************************************************************/
  2. /*                           UNSCRAMBLE Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".wds"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34.  
  35. #define BUFFERSIZE 16384
  36.  
  37. char ad[] =
  38. "UNSCRAMBLE utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  39.  
  40.  
  41.  
  42. void getword( char *letter_set, size_t w_len );
  43. void center( char *strng );
  44.  
  45. typedef enum { FALSE, TRUE } Boolean;
  46.  
  47. void main( int argc, char **argv )
  48. {
  49.  
  50.    char letterset[ MAXLEN ];
  51.  
  52.      if( argc == NOARGS )
  53.         {
  54.         clrscr();
  55.         puts( "Enter a LETTERSET to test ... " );
  56.         gets( letterset );
  57.      getword( letterset, strlen( letterset ) );
  58.         }
  59.      else
  60.         {
  61.         strcpy( letterset, *( argv + 1 ) );
  62.         getword( letterset, strlen( *( argv + 1 ) ) );
  63.         }
  64. }
  65.  
  66.  
  67.  
  68. Boolean wordtest( char *letterset, char *word )
  69. {
  70.     Boolean error_flag = TRUE;
  71.     static char dup_lset[ MAXLEN ];
  72.     register char *letpos;
  73.  
  74. //     dup_lset = strdup( letterset );
  75.      strcpy( dup_lset, letterset );
  76.          
  77.         while( *word )
  78.             {
  79.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  80.                 *letpos = '*';     //As long as letter contained...
  81.             else
  82.                 { error_flag = FALSE; break; } //test fails (not contained)
  83.             }
  84.  
  85.         return( error_flag );
  86. }
  87.  
  88. /*************************************************************/
  89. void getword( char *letter_set, size_t w_len )
  90. {
  91.  
  92.     char    l_set [ MAXLEN ],
  93.         word [ MAXLEN ],
  94.         tempstr [ MAXLEN + 1 ],
  95.         bar [ LINE_LEN + 1 ],
  96.         double_bar [ LINE_LEN + 1 ],
  97.    ts [ MAXLEN ];
  98.  
  99.     FILE *fptr;
  100.     long wcount = 0L;
  101.  
  102.        memset( bar, '-', LINE_LEN );
  103.        *( bar + LINE_LEN ) = NULL;
  104.        memset( double_bar, '=', LINE_LEN );
  105.        *( double_bar + LINE_LEN ) = NULL;
  106.  
  107.        /*************opening credits*************/
  108.        clrscr();
  109.        printf( double_bar );
  110.        strcpy( tempstr, ad );
  111.        center ( tempstr );
  112.        printf( tempstr );
  113.        printf( CR );
  114.        printf( double_bar );
  115.        printf( CR );
  116.        /****************************************/
  117.  
  118.  
  119.        strcpy ( l_set, letter_set );
  120.        strcat ( letter_set, CR );
  121.  
  122.  
  123.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  124.          {
  125.          printf( "\7\7\7Cannot open Wordfile!" );
  126.          exit( FILE_OPENING_ERROR );
  127.          }
  128.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  129.          exit( FILE_OPENING_ERROR );
  130.  
  131.        /**************'Wait' Message************/
  132.        printf( CR CR );
  133.        printf( "WORKING...\n\n" );
  134.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  135.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  136.        printf( "Now searching 99,000+ word file for possible solutions...\n\n" );
  137.        /*****************************************/
  138.  
  139.  
  140.  
  141.  
  142.  
  143.        sprintf( tempstr, "Words unscrambled from: %s\n", strupr( l_set ) );
  144.        center( tempstr );
  145.        printf( double_bar );
  146.        printf( tempstr );
  147.        printf( double_bar );
  148.        printf( CR );
  149.  
  150.  
  151.          /*********************Main Loop*************/     
  152.           while( fgets( word, MAXLEN, fptr ) != NULL )
  153.  
  154.             if( wordtest( letter_set, word ) )
  155.                if( strlen( word ) == w_len + INCREMENT )
  156.                  {
  157.                  printf( "%s", word );
  158.                  wcount++;
  159.                      }
  160.  
  161.       if( wcount == INCREMENT )
  162.          strcpy( ts, "word" );
  163.       else
  164.          strcpy( ts, "words" );
  165.  
  166.           /*******************************************/
  167.  
  168.           printf( bar );
  169.           sprintf( tempstr, "%ld %s can be unscrambled from %s.",
  170.                  wcount, ts, l_set );
  171.           center( tempstr );              
  172.           printf( tempstr );
  173.           printf( "\n\n" );
  174.  
  175.           center( ad );
  176.           printf( ad );
  177.       printf( "\7\n\n" );
  178.  
  179.           fcloseall();
  180.  
  181.  
  182. }
  183.  
  184.  
  185.  
  186. void center( char *str )
  187. {
  188.    int padding;
  189.    char st [ LINE_LEN + INCREMENT ];
  190.  
  191.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  192.      memset( st, SPACE, padding );
  193.      *( st + padding ) = NULL;  //Terminate string
  194.      strcat( st, str );
  195.      strcpy( str, st );
  196.  
  197.      return;
  198. }
  199.